package com.bsoft.gol.hbs.bbpmessagel.template;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

import java.lang.reflect.Type;
import java.util.Iterator;

/**
 * @author tongtp
 * @version v0.1
 * @Package com.bsoft.platform.service.sendMsg.impl.weChat.template
 * @className WxMiniSubscribeMessageGsonAdapter
 * @description 微信小程序订阅消息Gson适配类
 * @create 2020-10-16 14:33
 **/
public class WxMiniSubscribeMessageGsonAdapter implements JsonSerializer<WxMiniSubscribeMessage> {

	public WxMiniSubscribeMessageGsonAdapter() {
	}

	@Override
	public JsonElement serialize(WxMiniSubscribeMessage wxMiniSubscribeMessage, Type type,
                                 JsonSerializationContext jsonSerializationContext) {
		JsonObject messageJson = new JsonObject();
		messageJson.addProperty("touser", wxMiniSubscribeMessage.getToUser());
		messageJson.addProperty("template_id", wxMiniSubscribeMessage.getTemplateId());
		if (wxMiniSubscribeMessage.getPage() != null) {
			messageJson.addProperty("page", wxMiniSubscribeMessage.getPage());
		}
		messageJson.addProperty("miniprogram_state", wxMiniSubscribeMessage.getMiniprogramState());
		if (wxMiniSubscribeMessage.getLang() != null) {
			messageJson.addProperty("lang", wxMiniSubscribeMessage.getLang());
		}
		messageJson.addProperty("miniprogram_state", wxMiniSubscribeMessage.getMiniprogramState());

		JsonObject data = new JsonObject();
		messageJson.add("data", data);

		WxMiniSubscribeData datum;
		JsonObject dataJson;
		for (Iterator var6 = wxMiniSubscribeMessage.getData().iterator(); var6.hasNext(); data.add(datum.getName(),
				dataJson)) {
			datum = (WxMiniSubscribeData) var6.next();
			dataJson = new JsonObject();
			dataJson.addProperty("value", datum.getValue());
		}

		return messageJson;
	}
}
